package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.businessService.SecurityRoleService;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.menu.DispatchActionRequest;
import com.ktwlsoft.archivemanage.service.security.UserAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(value = "菜单与行为定义", description = "菜单与行为")
@RestController
@RequestMapping("/menuAndAction")
public class MenuAndActionController {

    private final SecurityRoleService securityRoleService;

    public MenuAndActionController(SecurityRoleService securityRoleService) {
        this.securityRoleService = securityRoleService;
    }

    @ApiOperation("角色分配行为")
    @PostMapping("/dispatchAction")
    @UserAuthenticate(actionCode = "ROLE_RIGHT")
    public ResponseEntity<?> dispatchAction(@RequestBody @Valid DispatchActionRequest request) {
        securityRoleService.dispatchActions(request);
        return ResponseEntity.ok(Result.ok("操作完成"));
    }

    @ApiOperation("查询角色所有的行为")
    @GetMapping("/findActionByRole")
    public ResponseEntity<?> findActionByRole(Long roleId) {
        return ResponseEntity.ok(Result.ok(securityRoleService.findActionsByRoleId(roleId)));
    }

    @ApiOperation("查询角色所有的菜单")
    @GetMapping("/findMenuByRole")
    public ResponseEntity<?> findMenuByRole(Long roleId) {
        return ResponseEntity.ok(Result.ok(securityRoleService.findMenusByRoleId(roleId)));
    }

    @ApiOperation("查询所有的菜单")
    @GetMapping("/findAllMenu")
    public ResponseEntity<?> findAllMenu(){
        return ResponseEntity.ok(Result.ok(securityRoleService.findAllMenu()));
    }

    @ApiOperation("查询所有的行为")
    @GetMapping("/findAllAction")
    public ResponseEntity<?> findAllAction(){
        return ResponseEntity.ok(Result.ok(securityRoleService.findAllAction()));
    }
}
